package com.wolfpire.news.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.news.dao.NewDao;
import com.wolfpire.notice.dao.NoticeDao;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.common.util.MatchType;
import com.wolfpire.system.model.New;
import com.wolfpire.system.model.Notice;
import com.wolfpire.system.model.Profession;
@Repository("newDao")
public class NewDaoImpl extends BaseHibernateDao<New, Long> 
	implements NewDao {

	public New getByTitle(String title) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<New> list(New news) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<New> findNews(Page<New> page, New filterNew) {
		Criterion[] criterions = this.createFilter(filterNew);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		if (0 < totalCount) {
			Criteria c = createCriteria(criterions);
			//c.addOrder(Order.asc("seq"));
			//c.addOrder(Order.asc("createTime"));                //由小到达
			c.addOrder(Order.desc("createTime"));                 //由大到小            
			c = setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<New> news = c.list();
			page.setDataList(news);
		}
		return page;
	}
	
	private Criterion[] createFilter(New news) {
		if (null == news) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != news.getId()) {
			criterions.add(Restrictions.eq("id", news.getId()));
		}
		if (!StringUtils.isBlank(news.getTitle())) {
			criterions.add(Restrictions.like("title", news.getTitle(), MatchMode.ANYWHERE));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	public List<New> list(int num) {
		StringBuffer querySql = new StringBuffer();
		querySql.append(" select * from t_new n ORDER BY create_time limit ");
		querySql.append(num);
		SQLQuery query = this.createSqlQuery(querySql.toString(), null, null);
		query.addEntity(New.class);
		return query.list();
	}

	public New findFrontNew(long id) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_new n where n.id = (select max(id) from t_new where id < :id )");
		propertyNames.add("id");
		propertyValues.add(id);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(New.class);
		return (New) query.uniqueResult();
	}

	public New findNextNew(long id) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_new n where n.id = (select min(id) from t_new where id > :id) ");
		propertyNames.add("id");
		propertyValues.add(id);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(New.class);
		return (New) query.uniqueResult();
	}

	public New currentNew(long id) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_new n where n.id = :id");
		propertyNames.add("id");
		propertyValues.add(id);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(New.class);
		return (New) query.uniqueResult();
	}
	
}
